package com.cndw.kongfu.ui;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

class DefaultCellEditor extends AbstractCellEditor implements TableCellEditor {
    /**
	 * 
	 */
	private static final long serialVersionUID = -1861675955630798493L;
	protected JComponent editorComponent;
    protected EditorDelegate delegate;
    protected int clickCountToStart = 1;
    public DefaultCellEditor(JButton button) {
        editorComponent = button;
        editorComponent.setRequestFocusEnabled(false);
        delegate = new EditorDelegate() {
            public void setValue(Object value) {
                if(value==null){
                    ((JButton)editorComponent).setText("");
                }
                else{
                    String text=value.toString();
                    ((JButton)editorComponent).setText(text);
                }
            }
            public Object getCellEditorValue() {
                return ((JButton)editorComponent).getText();
            }
        };
    }
    public Component getComponent() {
        return editorComponent;
    }
    public void setClickCountToStart(int count) {
        clickCountToStart = count;
    }
    public int getClickCountToStart() {
        return clickCountToStart;
    }
    public Object getCellEditorValue() {
        return delegate.getCellEditorValue();
    }
    public boolean isCellEditable(EventObject anEvent) {
        return delegate.isCellEditable(anEvent);
    }
    public boolean shouldSelectCell(EventObject anEvent) {
        return delegate.shouldSelectCell(anEvent);
    }
    public boolean stopCellEditing() {
        return delegate.stopCellEditing();
    }
    public void cancelCellEditing() {
        delegate.cancelCellEditing();
    }
    public Component getTableCellEditorComponent(JTable table, Object value,
                                                 boolean isSelected,
                                                 int row, int column) {
        if(value==null){
            return editorComponent;
        }
        delegate.setValue(value);
        return editorComponent;
    }
    protected class EditorDelegate{
        protected Object value;
        public Object getCellEditorValue() {
            return value;
        }
        public void setValue(Object value) {
            this.value = value;
        }
        public boolean isCellEditable(EventObject anEvent) {
            if (anEvent instanceof MouseEvent) {
                return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
            }
            return true;
        }
        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        }
        public boolean startCellEditing(EventObject anEvent) {
            return true;
        }
        public boolean stopCellEditing() {
            fireEditingStopped();
            return true;
        }
       public void cancelCellEditing() {
           fireEditingCanceled();
       }
    }
}